package com.hc.baselibray.navigationBar;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

//基类
public abstract class AbsNavigationBar <P extends  AbsNavigationBar.Builder.AbsNavigationParams> implements  INavigationBar{
    private final P  mParams;
   private View navigationView;
    public P getmParams() {
        return mParams;
    }


    protected void setText(int viewId, String txt) {
       TextView tv= findViewById(viewId);
       if(!TextUtils.isEmpty(txt)){
            tv.setVisibility(View.VISIBLE);
            tv.setText(txt);
       }
    }
    protected  void setVisible(int viewId,boolean bool){
        View tv= findViewById(viewId);
        tv.setVisibility(bool?View.VISIBLE:View.GONE);
    }
    protected   <T extends  View> T findViewById(int viewId){
        return (T)navigationView.findViewById(viewId);
    }

    protected  void  setOnClickListener(int viewId,View.OnClickListener listener){
        findViewById(viewId).setOnClickListener(listener);
    }

    //这里有个疑惑点，这个内部类本来就是在这个class里面声明的，为什么还要在外面传递进来
    //只是声明使用？和层级没有关系？
    //接受参数，和绑定视图
    public AbsNavigationBar(P mParams) {
        this.mParams = mParams;
        createAndBindView();
    }
    //绑定和创建view
    private void createAndBindView() {
        //创建view
        //第三个元素和换肤有关系
        navigationView= LayoutInflater.from(mParams.mContext).inflate(bindLayoutId(),mParams.mParent,false);
  //2.添加,到第0个位置
        mParams.mParent.addView(navigationView,0);
        applyView();

    }

    @Override
    abstract  public int bindLayoutId();

    @Override
   abstract public void applyView();
    public  static abstract class Builder{
        AbsNavigationParams P;

        public Builder(Context context, ViewGroup parent) {

        }
        public Builder(Context context) {

        }

        public  abstract  AbsNavigationBar builder();
        public static  class AbsNavigationParams{
            public  Context mContext;
            //插入当前navBar的父节点
            public  ViewGroup mParent;
           public AbsNavigationParams(Context context, ViewGroup parent){
                this.mContext=context;
                this.mParent=parent;
            }
            public AbsNavigationParams(Context context){
                this.mContext=context;
                Activity activity=(Activity) context;
                this.mParent=activity.findViewById(android.R.id.content);
            }
        }

    }
}
